import axios from 'axios'
import qs from 'qs';
import store from '../store'
axios.defaults.withCredentials = true //前后端分离session保持一致
export const qss = params => {return qs.stringify(params)}
const service = axios.create({
    baseURL:process.env.VUE_APP_DOMAIN,
    timeout:15000
})
// request拦截器
service.interceptors.request.use(config => {
    if (store.getters.loginToken) {
        config.headers['X-Nideshop-Token'] = store.getters.loginToken // 让每个请求携带自定义token 请根据实际情况自行修改
    }
    return config
}, error => {
    // Do something with request error
    console.log(error) // for debug
    Promise.reject(error)
})

// respone拦截器
service.interceptors.response.use(
    response => {
        /**
         * code为非20000是抛错 可结合自己业务进行修改
         */
        if(response.data.code != 10000){
            Promise.reject(response.data.msg)
        }
        return response.data
    },
    error => {
        console.log('err' + error)// for debug
        return Promise.reject(error)
    }
)

export default service
